from typing import List, Dict, Any, Optional, Union
from pydantic import BaseModel, Field
from datetime import datetime


class ResponseBase(BaseModel):
    """基础响应模型"""
    code: int = 200
    message: str = "success"


class SchoolBase(BaseModel):
    """学校基础模型"""
    name: str


class SchoolCreate(SchoolBase):
    """创建学校请求模型"""
    pass


class SchoolResponse(SchoolBase):
    """学校响应模型"""
    id: int
    created_at: Optional[datetime] = None

    class Config:
        from_attributes = True


class SchoolListResponse(ResponseBase):
    """学校列表响应模型"""
    data: List[SchoolResponse]


class ClassBase(BaseModel):
    """班级基础模型"""
    class_code: str
    class_name: str
    college_name: str
    grade_name: str
    major_code: str
    major_name: str
    major_direction: Optional[str] = None
    education_level: str
    student_count: Optional[str] = "0"
    academic_year_code: str
    academic_year_name: str
    term_code: str
    campus_id: str
    campus_name: str


class ClassCreate(ClassBase):
    """创建班级请求模型"""
    school_id: int


class ClassResponse(ClassBase):
    """班级响应模型"""
    id: int
    school_id: int
    created_at: Optional[datetime] = None

    class Config:
        from_attributes = True


class ClassInfo(BaseModel):
    """班级信息模型"""
    class_code: str
    class_name: str
    college_name: str


class ClassFilters(BaseModel):
    """班级筛选条件模型"""
    academic_years: List[str]
    terms: List[str]
    campuses: List[str]
    colleges: List[str]
    grades: List[str]
    majors: List[str]
    education_levels: List[str]


class ClassFiltersResponse(ResponseBase):
    """班级筛选条件响应模型"""
    data: ClassFilters


class ClassListResponse(ResponseBase):
    """班级列表响应模型"""
    data: Dict[str, Any]


class TeacherInfo(BaseModel):
    """教师信息模型"""
    name: str


class SectionInfo(BaseModel):
    """课程节次信息模型"""
    start: int
    end: int


class CourseBase(BaseModel):
    """课程基础模型"""
    course_name: str
    course_name_abbr: Optional[str] = None
    teacher: TeacherInfo
    day_of_week: int
    dateDigitSeparator: str
    campus: str
    location: str
    section: SectionInfo
    weeks: List[int]
    className: str
    all_classes: List[str]
    courseType: Optional[str] = None
    credit: Optional[str] = None
    classHour: Optional[str] = None
    studentCount: Optional[str] = "0"
    assessment: Optional[str] = None
    teachingMethod: Optional[str] = None
    bremark: Optional[Dict[str, Any]] = None


class CourseCreate(CourseBase):
    """创建课程请求模型"""
    class_id: int
    class_code: str


class CourseResponse(CourseBase):
    """课程响应模型"""
    id: int
    class_id: int
    class_code: str
    created_at: Optional[datetime] = None

    class Config:
        from_attributes = True


class CourseInfo(BaseModel):
    """课程信息模型"""
    course_name: str
    teacher: TeacherInfo
    day_of_week: int
    location: str
    section: SectionInfo
    weeks: List[int]
    courseType: Optional[str] = None


class ClassCoursesResponse(ResponseBase):
    """班级课程表响应模型"""
    data: Dict[str, Any]


class UserBase(BaseModel):
    """用户基础模型"""
    username: str


class UserCreate(UserBase):
    """用户创建模型"""
    password: str
    role: str = "admin"


class UserAuth(BaseModel):
    """用户认证请求模型"""
    username: str
    password: str


class UserInfo(BaseModel):
    """用户信息模型"""
    id: int
    username: str
    role: str


class TokenResponse(BaseModel):
    """令牌响应模型"""
    token: str
    expires_in: int
    user: UserInfo


class AuthResponse(ResponseBase):
    """认证响应模型"""
    data: TokenResponse


class ErrorResponse(BaseModel):
    """错误响应模型"""
    code: int
    message: str
    errors: Optional[Dict[str, List[str]]] = None
